package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Cons;
import cn.itsource.ymcc.domain.MediaFile;
import cn.itsource.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "VideoPushStreamConsumer",
        topic = Cons.VideoStream.TOPIC_COURSE_VIDEO_PUSH_STREAM,
        selectorExpression = Cons.VideoStream.TAGS_COURSE_VIDEO_PUSH_STREAM
)
public class VideoPushStreamConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt message) {
        try {
            log.info("媒体中心消费者正在接收视频异步推流消息。。。。。。");
            byte[] body = message.getBody();
            String jsonStr = new String(body, "utf-8");
            //json字符串反转为实体类对象
            MediaFile mediaFile = JSON.parseObject(jsonStr, MediaFile.class);
            //开始使用ffmpeg推流到云服务器
            mediaFileService.handleFile2m3u8(mediaFile);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
